<?php namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Game;
use Illuminate\Http\Request;
use Jenssegers\Agent\Facades\Agent;

class GameController extends Controller
{

    public function __construct()
    {

    }


    public function index(Request $request){
        $games = Game::latest()->take(20)->get();
        $title = "全部游戏";
        return view('frontend.games.index', compact('games', 'title'));
    }

    public function latest(Request $request){
        $games = Game::orderBy('download_counts')->take(20)->get();
        $title = "最新游戏";
        return view('frontend.games.index', compact('games', 'title'));
    }

    public function hot(Request $request){
        $games = Game::orderBy('download_counts')->take(20)->get();
        $title = "热门游戏";
        return view('frontend.games.index', compact('games', 'title'));
    }

    public function cat($category = null){
        if ($category){
            $category = Category::where('slug', $category)->first();
            if($category){
                $games = Game::latest()->where('category_slug', $category->slug)->paginate(20);
                $title = $category->name;
                return view('frontend.games.index', compact('games', 'title'));
            }
        }else{
            $title = "分类";
            $categories = Category::all();
            return view('frontend.pages.category', compact('categories', 'title'));
        }
    }
    public function show($id)
    {
        $game = Game::find($id);
        return view('frontend.games.show', compact('game'));
    }


    /**
     * 游戏下载
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function download($id){
        if($id){
            $game = Game::find($id);
            if($game){
                $game->increment('download_counts');
                return view('frontend.games.jump', compact('game'));
            }
        }
    }
}